//
//  Subject.m
//  观察者模式
//
//  Created by flannery on 2018/12/19.
//  Copyright © 2018年 flannery. All rights reserved.
//

#import "Subject.h"
#import "Observer.h"

@implementation Subject

- (NSMutableArray *)observers{
    if(!_observers) {
        _observers = [[NSMutableArray alloc]init];
    }
    return _observers;
}

- (void)setState:(int)state{
    _state = state;
    [self notifyAllObservers];
}


- (void)attach:(Observer *)observer{
    [self.observers addObject:observer];
}

- (void)notifyAllObservers{
    for (Observer *observer in self.observers) {
        [observer update];
    }
}

@end
